/***智能过滤*/
var _ = require('../../FrameWork/we_underscore');
var FindBomb = require('./FindBomb');
var EumPkGroup = require('./../Enum/EumPkGroup');
var FindPairsTool = require('./FindPairsTool');
var FindShunZiTool = require('./FindShunZiTool');
var FindThree = require('./FindThree');
var FindFour = require('./FindFour');

var FindAutoFilter = module.exports = {};

/***自动帅选更大的牌型
 * _lastOutCardInfo:{seatIdx:0,type:1,cardIds:[1,2,3]}当前玩家已出的牌信息
 * _choiceCardsInfo:[{cardId:1,value:1},{cardId:1,value:1}]自己操作选中的牌
 */
FindAutoFilter.getBigCardsType = function(_lastOutCardInfo, _choiceCardsInfo) {

};

/***自己手动操作选中后重新帅选一次
 * _choiceCardsInfo:[{cardId:1,value:1},{cardId:1,value:1}]自己操作选中的牌
 */
FindAutoFilter.getAutoChoiceBastType = function(_choiceCardsInfo) {
    var size = _.size(_choiceCardsInfo);
    var canGroupTypeList = this.getOptimalType(size);
    var typeSize = _.size(canGroupTypeList);
    for (var i = 0; i < typeSize; ++i) {
        var _type = canGroupTypeList[i];
        var result = FindAutoFilter.getRight(_choiceCardsInfo, _type);
        if (result.isFind) {
            return result;
        }
    }

    return {
        isFind: false,
        findGroup: [],
        otherGroup: _choiceCardsInfo
    }
};

/***模糊选择时排出最优牌型
 * _cardCnt:牌数量
 */
FindAutoFilter.getOptimalType = function(_cardCnt) {
    var infos = [
        //1张牌
        [EumPkGroup.ONE],
        //2张牌 
        [EumPkGroup.PAIRS, EumPkGroup.ONE, EumPkGroup.BOMBKING],
        //3张牌 
        [EumPkGroup.THREE, EumPkGroup.PAIRS, EumPkGroup.ONE, EumPkGroup.BOMBKING],
        //4张牌 
        [EumPkGroup.THREEONE, EumPkGroup.PAIRS, EumPkGroup.ONE, EumPkGroup.BOMB, EumPkGroup.BOMBKING],
        //5张牌 
        [EumPkGroup.STRAIGHT, EumPkGroup.THREEPAIRS, EumPkGroup.THREEONE, EumPkGroup.PAIRS, EumPkGroup.ONE, EumPkGroup.BOMB, EumPkGroup.BOMBKING],
        //6张牌 
        [EumPkGroup.STRAIGHT, EumPkGroup.EVENPAIRS, EumPkGroup.THREEPAIRS, EumPkGroup.THREEONE, EumPkGroup.PAIRS, EumPkGroup.ONE, EumPkGroup.BOMB, EumPkGroup.BOMBKING],
        //7张牌 
        [EumPkGroup.AIRCRAFT, EumPkGroup.STRAIGHT, EumPkGroup.EVENPAIRS, EumPkGroup.THREEPAIRS, EumPkGroup.THREEONE, EumPkGroup.PAIRS, EumPkGroup.ONE, EumPkGroup.BOMB, EumPkGroup.BOMBKING],
    ];
    var maxSize = _.size(infos);
    var idx = _cardCnt > maxSize ? maxSize : _cardCnt;
    idx -= 1;
    return infos[idx];
};

/*** 
    _cardsInfo:[{}]//卡牌信息
    return {
        isFind: true,
        findGroup: [],
        otherGroup: []
    }
*/
FindAutoFilter.getFind = function(_cardsInfo) {
    var cardsSize = _.size(_cardsInfo);
    var groups = this.getOptimalType(cardsSize);

    var groupSize = _.size(groups);
    for (var i = 0; i < groupSize; ++i) {
        var _type = groups[i];
        var _result = this.getRight(_cardsInfo, _type);
        if (_result.isFind) {
            return _result;
        }
    }
    return {
        isFind: true,
        findGroup: [],
        otherGroup: []
    }
};

/***获取修正过得牌
 * _minType:最小类型
 */
FindAutoFilter.getRight = function(_cardsInfo, _minType) {
    var size = _.size(_cardsInfo);
    //单牌
    if (EumPkGroup.ONE == _minType) {
        /***什么都没找到 */
        return {
            isFind: true,
            findGroup: _cardsInfo,
            otherGroup: []
        }
    } //对子
    else if (EumPkGroup.PAIRS == _minType) {
        return FindPairsTool.getResult(_cardsInfo);
    } //连对
    else if (EumPkGroup.EVENPAIRS == _minType) {
        return FindPairsTool.getEvenPairMax(_cardsInfo);
    } //三张
    else if (EumPkGroup.THREE == _minType) {
        return FindThree.getThreeResult(_cardsInfo);
    } //三带一
    else if (EumPkGroup.THREEONE == _minType) {
        return FindThree.getBeltOneMore(_cardsInfo);
    } //三带对子
    else if (EumPkGroup.THREEPAIRS == _minType) {
        return FindThree.getBeltPairsMore(_cardsInfo);
    } //三带二 
    else if (EumPkGroup.THREETWO == _minType) {

    } //顺子
    else if (EumPkGroup.STRAIGHT == _minType) {
        return FindShunZiTool.getMoreResultSection(_cardsInfo, 5, size);
    } //飞机 
    else if (EumPkGroup.AIRCRAFT == _minType) {
        return FindThree.getAircraftMore(_cardsInfo);
    } //四带一
    else if (EumPkGroup.FOURONE == _minType) {

    } //四带二
    else if (EumPkGroup.FOURTWO == _minType) {

    } //四带三 
    else if (EumPkGroup.FOURTHREE == _minType) {

    } //四带二对
    else if (EumPkGroup.FOURTWOPAIRS == _minType) {

    } //炸弹 
    else if (EumPkGroup.BOMB == _minType) {
        return FindFour.getResultBomb(_cardsInfo);
    } //王炸 
    else if (EumPkGroup.BOMBKING == _minType) {
        var resultTmp = FindBomb.getResultKing(_cardsInfo);
        if (resultTmp.isFind) {
            return resultTmp;
        }
    }
    /***什么都没找到 */
    return {
        isFind: false,
        findGroup: [],
        otherGroup: _cardsInfo
    }
};